Packages
Lecture 10
Review: figures
- plot(x = __, y = ___, main = “__”, xlab = “__”, ylab = “__”)
- boxplot(x = __, main = “__”, xlab = “__”, ylab = “__”)
- hist(x = __, main = “__”, xlab = “__”, ylab = “__”)
- install.packages("ggplot2") #installs this package to your R
- library(ggplot2) #opens the package for use
- ggplot(__, aes()) + geom_point() + geom_smooth() + labs()
- geom_boxplot()
- geom_histogram()
Today: Packages
- plot(x = __, y = ___, main = “__”, xlab = “__”, ylab = “__”)
- boxplot(x = __, main = “__”, xlab = “__”, ylab = “__”)
- hist(x = __, main = “__”, xlab = “__”, ylab = “__”)
- install.packages("ggplot2") #installs this package to your R
- library(ggplot2) #opens the package for use
- ggplot(__, aes()) + geom_point() + geom_smooth() + labs()
- geom_boxplot()
- geom_histogram()
What’s a Package?
- Package: “collections of functions and data sets
developed by the community” (https://www.datacamp.com/community/tutorials/r-packages-guide)
- So far, we have learned many methods:
- Ex: print(), sum(), summary(), mean(), etc
- Part of R’s base package: came with R
- What if we want to use other functions not part of base
package?
- Ex: ggplot(), geom_point(), geom_boxplot(), etc.
What’s a Package?
- Anyone can write a function that does something or create a
dataset
- If that function or dataset is really useful, want to share that
function with others
- Do this by putting functions/datasets into a package that
others can download
Installing packages
install.packages("____") #installs this package to your R
- Only need to install package once
library(___) #opens the package for use
- Load the library (tell R you want to use this package) each time
you open your script
Learning About a Package
- We learned about RDocumentation
- help(function_name) or ?function_name
- But when we first install a package, we might not know
what all the functions are called
- Reference manual: user manual to R package
- Lists all the functions and explains what each one does
- Like a giant combined RDocumentation for that package
- Vignette: helpful documentation/examples that walk you
through a tutorial
Package: ggplot2
- https://cran.r-project.org/web/packages/ggplot2/ggplot2.pdf
Package: ggplot2
- https://cran.r-project.org/web/packages/ggplot2/ggplot2.pdf
Package: readxl
- https://cran.r-
project.org/web/package
s/readxl/readxl.pdf
- We have been limited to
loading text file datasets
(.txt, .csv, .tsv, etc) so
far
- What if data was in
excel file?
Package: readxl
Package: readxl
Package: readxl
Your Turn: heart_excel
- Read in the heart_excel dataset, storing it in variable
heart
Hint:
File → import dataset → from Excel
Package: tableone
- https://cran.r-project.org/web/packages/tableone/tableone.pdf
Another helpful
resource:
https://cran.r-
project.org/web/packages/ta
bleone/vignettes/introduction
.html
Package: tableone
- table1 <- CreateTableOne(vars = all_variables_to_include,
- data = dataset_name,
- factorVars = categorical_variables,
- strata = stratifying_variables)
- print(table1, showAllLevels = TRUE or FALSE)
Package: tableone
Your Turn: heart_excel
- Create the following table
- Sex, BMI, smoking are categorized
- NA’s are included
Solution
Solution Cont.
Tableone with p-values
This week’s homework assignment:
Find a package that relates to your own
interests (due December 6)
Package: LDheatmap
https://cran.r-project.org/web/packages/LDheatmap/LDheatmap.pdf
Image credit:
https://www.biostars.org/p/379447/
LDheatmap(SNP data)
Slide from lecture 1: Other fun/irrelevant uses
- Generate random cat fact
- Generate random noises
- Play rap music
- Tweet
- Order an Uber
More useful packages
- ggplot2: figures
- tableone: create a typical “table 1”
- dplyr: data manipulation
- shiny: interactive figures
- rcpp: C++
- deSolve: differential equations
- data.table: to help work with datasets
- Many packages with functions that help with statistical
analyses
- Many many more
End of lecture material!
Thank you!!